#include "page_cha.h"

static lv_ft_info_t info1;
static lv_style_t style1;
static lv_obj_t * kb;
static lv_obj_t * ta1;
static lv_obj_t * list1;
static const char * btns[]  = {"确定", ""};
static const char * btns1[] = {""};
static lv_obj_t * msgbox;
static lv_obj_t * win;
static lv_obj_t *name, *price, *stock, *vername;
static lv_obj_t * pinyin;
static lv_obj_t * cand_panel;

static void ta_cb(lv_event_t * e);
static void btn_cb1(lv_event_t * e);
static void btn_cb2(lv_event_t * e);
static void select_cb(lv_event_t * e);
static void yes_cb(lv_event_t * e);
static void yy_cb(lv_event_t * e);
static void cha_cb(lv_event_t * e);
static void yyy_cb(lv_event_t * e);
static void can_cb(lv_event_t * e);
static void blank_cb(lv_event_t * e);
static void flash_list();

void flash_list()
{
    lv_obj_del(list1);
    show_list(&list1, page_cha, 650, 320, 20, 140, &style1);
    dlist * p = proinf->next;

    char buf[128] = {0};
    while(p != proinf) {
        snprintf(buf, sizeof(buf), "商品：%s  单价：%.2f  库存：%d  图片：%s", p->name, p->price, p->stock,
                 p->ver_name);
        lv_obj_t * btn = lv_list_add_btn(list1, NULL, buf);
        lv_obj_set_size(btn, 600, 60);

        p->btn1 = btn;
        lv_obj_add_event_cb(btn, select_cb, LV_EVENT_CLICKED, NULL);
        p = p->next;
    }
}

void yes_cb(lv_event_t * e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
    if(pinyin && lv_obj_is_valid(pinyin)) {
        lv_obj_add_flag(pinyin, LV_OBJ_FLAG_HIDDEN); // 隐藏拼音输入法
    }
    lv_obj_add_flag(cand_panel, LV_OBJ_FLAG_HIDDEN);
    dlist * p = (dlist *)e->user_data;

    double temp1 = atof(lv_textarea_get_text(price));
    int temp2    = atoi(lv_textarea_get_text(stock));

    const lv_img_dsc_t * img_dsc = get_img_dsc_by_ver_name(lv_textarea_get_text(vername));

    if(img_dsc == &wu) {
        msgbox = lv_msgbox_create(lv_scr_act(), "提示", "没有这个商品的图片,你要继续吗", btns, true);
        lv_obj_center(msgbox);
        lv_obj_t * title   = lv_msgbox_get_title(msgbox);
        lv_obj_t * content = lv_msgbox_get_text(msgbox);
        lv_obj_t * buttons = lv_msgbox_get_btns(msgbox);
        if(title) lv_obj_set_style_text_font(title, info1.font, 0);
        if(content) lv_obj_set_style_text_font(content, info1.font, 0);
        if(buttons) lv_obj_set_style_text_font(buttons, info1.font, 0);
        lv_obj_add_event_cb(buttons, yy_cb, LV_EVENT_CLICKED, p);
    }

    strcpy(p->name, lv_textarea_get_text(name));
    p->price = temp1;
    p->stock = temp2;
    strcpy(p->ver_name, lv_textarea_get_text(vername));
    p->img_dsc = img_dsc;
    if(win != NULL && lv_obj_is_valid(win)) { // 检查win是否存在且有效
        lv_obj_del(win);                      // 销毁旧弹窗
        win = NULL;                           // 重置全局变量
    }
    flash_list();
}

void yy_cb(lv_event_t * e)
{
    dlist * p    = (dlist *)e->user_data;
    double temp1 = atof(lv_textarea_get_text(price));
    int temp2    = atoi(lv_textarea_get_text(stock));

    const lv_img_dsc_t * img_dsc = get_img_dsc_by_ver_name(lv_textarea_get_text(vername));
    strcpy(p->name, lv_textarea_get_text(name));
    p->price = temp1;
    p->stock = temp2;
    strcpy(p->ver_name, lv_textarea_get_text(vername));
    p->img_dsc = img_dsc;
    if(win != NULL && lv_obj_is_valid(win)) { // 检查win是否存在且有效
        lv_obj_del(win);                      // 销毁旧弹窗
        win = NULL;                           // 重置全局变量
    }
    flash_list();
}

void cha_cb(lv_event_t * e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
    if(pinyin && lv_obj_is_valid(pinyin)) {
        lv_obj_add_flag(pinyin, LV_OBJ_FLAG_HIDDEN); // 隐藏拼音输入法
    }
    lv_obj_add_flag(cand_panel, LV_OBJ_FLAG_HIDDEN);
    dlist * p = (dlist *)e->user_data;

    double temp1 = atof(lv_textarea_get_text(price));
    int temp2    = atoi(lv_textarea_get_text(stock));

    const lv_img_dsc_t * img_dsc = get_img_dsc_by_ver_name(lv_textarea_get_text(vername));

    if(img_dsc == &wu) {
        msgbox = lv_msgbox_create(lv_scr_act(), "提示", "没有这个商品的图片,你要继续吗", btns, true);
        lv_obj_center(msgbox);
        lv_obj_t * title   = lv_msgbox_get_title(msgbox);
        lv_obj_t * content = lv_msgbox_get_text(msgbox);
        lv_obj_t * buttons = lv_msgbox_get_btns(msgbox);
        if(title) lv_obj_set_style_text_font(title, info1.font, 0);
        if(content) lv_obj_set_style_text_font(content, info1.font, 0);
        if(buttons) lv_obj_set_style_text_font(buttons, info1.font, 0);
        lv_obj_add_event_cb(buttons, yyy_cb, LV_EVENT_CLICKED, p);
    } else {
        strcpy(p->name, lv_textarea_get_text(name));
        p->price = temp1;
        p->stock = temp2;
        strcpy(p->ver_name, lv_textarea_get_text(vername));
        p->img_dsc = img_dsc;
        writetxt();
        if(win != NULL && lv_obj_is_valid(win)) { // 检查win是否存在且有效
            lv_obj_del(win);                      // 销毁旧弹窗
            win = NULL;                           // 重置全局变量
        }
        flash_list();
    }
}

void yyy_cb(lv_event_t * e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
    if(pinyin && lv_obj_is_valid(pinyin)) {
        lv_obj_add_flag(pinyin, LV_OBJ_FLAG_HIDDEN); // 隐藏拼音输入法
    }
    lv_obj_add_flag(cand_panel, LV_OBJ_FLAG_HIDDEN);
    dlist * p    = (dlist *)e->user_data;
    double temp1 = atof(lv_textarea_get_text(price));
    int temp2    = atoi(lv_textarea_get_text(stock));

    const lv_img_dsc_t * img_dsc = get_img_dsc_by_ver_name(lv_textarea_get_text(vername));
    strcpy(p->name, lv_textarea_get_text(name));
    p->price = temp1;
    p->stock = temp2;
    strcpy(p->ver_name, lv_textarea_get_text(vername));
    p->img_dsc = img_dsc;
    writetxt();
    if(win != NULL && lv_obj_is_valid(win)) { // 检查win是否存在且有效
        lv_obj_del(win);                      // 销毁旧弹窗
        win = NULL;                           // 重置全局变量
    }
    flash_list();
}

void can_cb(lv_event_t * e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
    if(pinyin && lv_obj_is_valid(pinyin)) {
        lv_obj_add_flag(pinyin, LV_OBJ_FLAG_HIDDEN); // 隐藏拼音输入法
    }
    lv_obj_add_flag(cand_panel, LV_OBJ_FLAG_HIDDEN);
    if(win != NULL && lv_obj_is_valid(win)) { // 检查win是否存在且有效
        lv_obj_del(win);                      // 销毁旧弹窗
        win = NULL;                           // 重置全局变量
    }
}

void ta_cb(lv_event_t * e)
{
    lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);     // 取消隐藏键盘
    lv_obj_clear_flag(pinyin, LV_OBJ_FLAG_HIDDEN); // 取消隐藏拼音输入法

    lv_keyboard_set_textarea(kb, e->target);

    // 将键盘和拼音输入法移到最前面
    lv_obj_move_foreground(kb);
    if(pinyin && lv_obj_is_valid(pinyin)) {
        lv_obj_move_foreground(pinyin);
    }
    lv_obj_move_foreground(cand_panel);
}

void btn_cb2(lv_event_t * e)
{
    page2_show();
    lv_obj_del(page_cha);
}

void blank_cb(lv_event_t * e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
    if(pinyin && lv_obj_is_valid(pinyin)) {
        lv_obj_add_flag(pinyin, LV_OBJ_FLAG_HIDDEN); // 隐藏拼音输入法
    }
    lv_obj_add_flag(cand_panel, LV_OBJ_FLAG_HIDDEN);
}

void select_cb(lv_event_t * e)
{
    dlist * a = proinf;
    char temp1[30];
    char temp2[30];
    while(a->next != proinf) {
        a = a->next;
        if(a->btn1 == e->target) break;
    }
    if(win != NULL && lv_obj_is_valid(win)) { // 检查win是否存在且有效
        lv_obj_del(win);                      // 销毁旧弹窗
        win = NULL;                           // 重置全局变量
    }
    win = lv_obj_create(page_cha);
    lv_obj_set_size(win, 600, 280);
    lv_obj_set_pos(win, 100, 20);

    show_label(win, 60, 60, 20, 35, &style1, "名字");
    show_ta(&name, win, 120, 60, 90, 20, &style1);
    lv_textarea_add_text(name, a->name);
    lv_obj_add_event_cb(name, ta_cb, LV_EVENT_FOCUSED, NULL);

    show_label(win, 60, 60, 280, 35, &style1, "单价");
    show_ta(&price, win, 120, 60, 360, 20, &style1);
    snprintf(temp1, sizeof(temp1), "%f", a->price);
    lv_textarea_add_text(price, temp1);
    lv_textarea_set_accepted_chars(price, "0123456789.");
    lv_textarea_set_max_length(price, 6);
    lv_obj_add_event_cb(price, ta_cb, LV_EVENT_FOCUSED, NULL);

    show_label(win, 60, 60, 20, 115, &style1, "库存");
    show_ta(&stock, win, 120, 60, 90, 100, &style1);
    snprintf(temp2, sizeof(temp2), "%d", a->stock);
    lv_textarea_add_text(stock, temp2);
    lv_textarea_set_accepted_chars(stock, "0123456789");
    lv_textarea_set_max_length(stock, 6);
    lv_obj_add_event_cb(stock, ta_cb, LV_EVENT_FOCUSED, NULL);

    show_label(win, 60, 60, 280, 115, &style1, "图片");
    show_ta(&vername, win, 120, 60, 360, 100, &style1);
    lv_textarea_add_text(vername, a->ver_name);
    lv_obj_add_event_cb(vername, ta_cb, LV_EVENT_FOCUSED, NULL);

    lv_obj_t * btn1 = show_btn_label(win, 50, 40, 20, 180, &style1, "更改");
    lv_obj_add_event_cb(btn1, yes_cb, LV_EVENT_CLICKED, a);
    lv_obj_t * btn3 = show_btn_label(win, 100, 40, 100, 180, &style1, "永久更改");
    lv_obj_add_event_cb(btn3, cha_cb, LV_EVENT_CLICKED, a);
    lv_obj_t * btn2 = show_btn_label(win, 50, 40, 230, 180, &style1, "取消");
    lv_obj_add_event_cb(btn2, can_cb, LV_EVENT_CLICKED, NULL);
}

void btn_cb1(lv_event_t * e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
    if(pinyin && lv_obj_is_valid(pinyin)) {
        lv_obj_add_flag(pinyin, LV_OBJ_FLAG_HIDDEN); // 隐藏拼音输入法
    }
    lv_obj_add_flag(cand_panel, LV_OBJ_FLAG_HIDDEN);
    char buf[50];
    char temp[128];
    strcpy(buf, lv_textarea_get_text(ta1));
    dlist * a = proinf->next;
    int test  = 0;
    if(strcmp(buf, "") == 0) {
        flash_list();
        return;
    }
    if(list1 != NULL && lv_obj_is_valid(list1)) { // 检查list1是否存在且有效
        lv_obj_del(list1);                        // 销毁旧弹窗
        list1 = NULL;                             // 重置全局变量
    }
    show_list(&list1, page_cha, 650, 320, 20, 140, &style1);
    while(a != proinf) {
        if(strstr(a->name, buf) != NULL) {
            test = 1;
            snprintf(temp, sizeof(temp), "商品：%s  单价：%.2f  库存：%d  图片：%s", a->name, a->price, a->stock,
                     a->ver_name);
            lv_obj_t * btn = lv_list_add_btn(list1, NULL, temp);
            lv_obj_set_size(btn, 600, 60);

            a->btn1 = btn;
            lv_obj_add_event_cb(btn, select_cb, LV_EVENT_CLICKED, NULL);
        }
        a = a->next;
    }
    if(test == 0) {
        msgbox = lv_msgbox_create(lv_scr_act(), "提示", "没有这个商品", btns1, true);
        lv_obj_center(msgbox);
        lv_obj_t * title   = lv_msgbox_get_title(msgbox);
        lv_obj_t * content = lv_msgbox_get_text(msgbox);
        if(title) lv_obj_set_style_text_font(title, info1.font, 0);
        if(content) lv_obj_set_style_text_font(content, info1.font, 0);
    }
}

void page_cha_show()
{
    // 中文字库加载
    /*FreeType uses C standard file system, so no driver letter is required.*/
    info1.name   = "/font/simkai.ttf";
    info1.weight = 24;
    info1.style  = FT_FONT_STYLE_NORMAL;
    info1.mem    = NULL;
    if(!lv_ft_font_init(&info1)) {
        LV_LOG_ERROR("create failed.");
    }
    /*Create style with the new font*/
    lv_style_init(&style1);
    lv_style_set_text_font(&style1, info1.font);
    lv_style_set_text_align(&style1, LV_TEXT_ALIGN_CENTER);

    show_page(&page_cha);

    show_ta(&ta1, page_cha, 550, 60, 20, 40, &style1);
    lv_obj_add_event_cb(ta1, ta_cb, LV_EVENT_FOCUSED, NULL);

    lv_obj_t * btn1 = show_btn_label(page_cha, 80, 80, 590, 30, &style1, "搜索");
    lv_obj_add_event_cb(btn1, btn_cb1, LV_EVENT_CLICKED, NULL);

    show_list(&list1, page_cha, 650, 320, 20, 140, &style1);
    dlist * p = proinf->next;

    char buf[128] = {0};
    while(p != proinf) {
        snprintf(buf, sizeof(buf), "商品：%s  单价：%.2f  库存：%d  图片：%s", p->name, p->price, p->stock,
                 p->ver_name);
        lv_obj_t * btn = lv_list_add_btn(list1, NULL, buf);
        lv_obj_set_size(btn, 600, 60);

        p->btn1 = btn;
        lv_obj_add_event_cb(btn, select_cb, LV_EVENT_CLICKED, NULL);
        p = p->next;
    }

    lv_obj_t * btn2 = show_btn_label(page_cha, 80, 80, 700, 380, &style1, "返回");
    lv_obj_add_event_cb(btn2, btn_cb2, LV_EVENT_CLICKED, NULL);

    kb = lv_keyboard_create(page_cha);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    lv_obj_set_size(kb, 800, 180);

    pinyin = lv_ime_pinyin_create(page_cha);
    lv_obj_add_flag(pinyin, LV_OBJ_FLAG_HIDDEN);
    lv_ime_pinyin_set_keyboard(pinyin, kb);
    lv_ime_pinyin_set_dict(pinyin, my_dict);
    // 设置拼音输入法的字典
    lv_obj_set_style_text_font(pinyin, info1.font, 0);

    // 获取候选词面板并应用样式
    cand_panel = lv_ime_pinyin_get_cand_panel(pinyin);
    if(cand_panel) {
        // 1. 强制设置面板主体（LV_PART_MAIN）的背景和边框（优先级最高）
        lv_obj_set_style_bg_opa(cand_panel, LV_OPA_100, LV_PART_MAIN);                   // 完全不透明（关键！）
        lv_obj_set_style_bg_color(cand_panel, lv_color_hex(0xFFFFFF), LV_PART_MAIN);     // 白色背景
        lv_obj_set_style_border_width(cand_panel, 2, LV_PART_MAIN);                      // 边框宽度
        lv_obj_set_style_border_color(cand_panel, lv_color_hex(0xFF0000), LV_PART_MAIN); // 红色边框（便于测试）
        lv_obj_set_style_radius(cand_panel, 5, LV_PART_MAIN);                            // 圆角
        lv_obj_set_height(cand_panel, 60);                                               // 增加面板高度
        lv_obj_align_to(cand_panel, kb, LV_ALIGN_OUT_TOP_MID, 0, 0);                     // 增加垂直偏移量

        // 2. 强制设置内部候选词的样式（若有子控件）
        // lv_obj_t * child = lv_obj_get_child(cand_panel, 0);
        // while(child) {
        //     lv_obj_set_style_bg_opa(child, LV_OPA_100, LV_PART_MAIN);                 // 子控件背景不透明
        //     lv_obj_set_style_bg_color(child, lv_color_hex(0xFFFFE0), LV_PART_MAIN);   // 浅黄色背景
        //     lv_obj_set_style_text_color(child, lv_color_hex(0x000000), LV_PART_MAIN); // 黑色文字
        //     child = lv_obj_get_child(cand_panel, lv_obj_get_index(child) + 1);
        // }

        // 3. 确保面板在最顶层，避免被其他控件遮挡
        lv_obj_move_foreground(cand_panel);
        // 4. 强制刷新，立即生效
        lv_obj_invalidate(cand_panel);
    }

    lv_obj_add_event_cb(page_cha, blank_cb, LV_EVENT_CLICKED, NULL);
}